package request

import (
	"livefind/model/evidences/query"
	"livefind/service/common"
	"livefind/utils/logger"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

// swagger:parameters EvidenceCreateReq
type EvidenceCreateReq struct {
	CaseID       string `json:"caseId,omitempty"`
	EvidenceNo   string `json:"evidenceNo,omitempty"`
	EvidenceName string `json:"evidenceName,omitempty"`
	EvidenceType string `json:"evidenceType,omitempty"`
	SavePath     string `json:"savePath"`
	AutoParse    bool   `json:"autoParse"`
}

// swagger:parameters EvidenceListReq
type EvidenceListReq struct {
	// 案件ID
	// example:"6123838275877d0f16e654f0"
	CaseId string `json:"caseId" form:"caseId"`
	// 页码
	// example:1
	Page int `json:"page" form:"page"`
	// 页面数据条数
	// example:10
	PageSize int `json:"pageSize" form:"pageSize"`
}

type EvidenceGetReq struct {
	// mongo ObjectID
	// required:true
	// example:"61320b17f9a1dc9e2257f77f"
	ID string `form:"id" binding:"required"`
}

func (req *EvidenceCreateReq) ConvertCreate2Query() (evidenceQuery *query.EvidenceQuery, err error) {
	id, err := primitive.ObjectIDFromHex(req.CaseID)
	if err != nil {
		logger.Error(err.Error(), err)
		return nil, err
	}
	evidenceQuery = &query.EvidenceQuery{
		CaseID:       id,
		EvidenceNo:   req.EvidenceNo,
		EvidenceName: req.EvidenceName,
		EvidenceType: req.EvidenceType,
		SavePath:     req.SavePath,
		AutoParse:    req.AutoParse,
	}
	return evidenceQuery, err
}

func (req *EvidenceListReq) ConvertList2Query() (evidenceQuery *query.EvidenceQuery, err error) {
	q := common.ParseQueryRule(req)
	id, err := primitive.ObjectIDFromHex(req.CaseId)

	if err != nil {
		logger.Error(err.Error(), err)
		return nil, err
	}

	evidenceQuery = &query.EvidenceQuery{
		CaseID: id,
		Query:  q,
	}

	return evidenceQuery, err
}

func (req *EvidenceGetReq) ConvertGetById2Query() (id primitive.ObjectID, err error) {
	id, err = primitive.ObjectIDFromHex(req.ID)
	return id, err
}
